package com.pay.paytoken.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.pay.paytoken.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/20.
 */

public class TokenCodeAdapter extends BaseAdapter {
    private int num;
    private int current = -1;
    private boolean isAll = false;

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        Holder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_token_code_tv, null);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        int imageResource;
        switch (position) {
            case 0:
                imageResource = getImageResource(num);
                break;
            case 1:
                imageResource = getImageResource(num);
                break;
            case 2:
                imageResource = getImageResource(num);
                break;
            case 3:
                imageResource = getImageResource(num);
                break;
            case 4:
                imageResource = getImageResource(num);
                break;
            case 5:
                imageResource = getImageResource(num);
                break;
            case 6:
                imageResource = getImageResource(num);
                break;
            case 7:
                imageResource = getImageResource(num);
                break;
            case 8:
                imageResource = getImageResource(num);
                break;
            case 9:
                imageResource = getImageResource(isAll ? num : current);
                current = num;
                isAll = false;
                break;
            default:
                imageResource = getImageResource(0);
                break;
        }
        holder.mImageView.setImageResource(imageResource);
        return convertView;
    }

    private int getImageResource(int num) {
        int imageResource;
        switch (num) {
            case 0:
                imageResource = R.drawable.num0;
                break;
            case 1:
                imageResource = R.drawable.num1;
                break;
            case 2:
                imageResource = R.drawable.num2;
                break;
            case 3:
                imageResource = R.drawable.num3;
                break;
            case 4:
                imageResource = R.drawable.num4;
                break;
            case 5:
                imageResource = R.drawable.num5;
                break;
            case 6:
                imageResource = R.drawable.num6;
                break;
            case 7:
                imageResource = R.drawable.num7;
                break;
            case 8:
                imageResource = R.drawable.num8;
                break;
            case 9:
                imageResource = R.drawable.num9;
                break;
            default:
                imageResource = R.drawable.num0;
                break;
        }
        return imageResource;
    }

    public void setNum(int num) {
        this.num = num;
        if (current == -1) {
            current = num;
        }
    }

    public void setAllNum() {
        isAll = true;
        notifyDataSetChanged();
    }

    public static class Holder {
        @Bind(R.id.token_code_imageView)
        ImageView mImageView;

        public Holder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
